package com.lzy.mall.auth.controller;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.lzy.mall.common.constant.AuthServerConstant;
import com.lzy.mall.auth.feign.MemberFeignService;
import com.lzy.mall.auth.feign.ThirdPartFeignService;
import com.lzy.mall.common.vo.MemberRespVo;
import com.lzy.mall.auth.vo.UserLoginVo;
import com.lzy.mall.auth.vo.UserRegisterVo;
import com.lzy.mall.common.utils.ResultUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录注册模块
 * @author lzy
 * @date 2021/9/24 11:35
 */
@Controller
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class LoginController {

    private final ThirdPartFeignService thirdPartFeignService;

    private final StringRedisTemplate redisTemplate;

    private final MemberFeignService memberFeignService;

    @ResponseBody
    @GetMapping("/sms/snedcode")
    public ResultUtils sendCode(@RequestParam("phone") String phone) {
        String code = UuidUtils.generateUuid().substring(0, 6);

        //TODO 手机短信验证
        //redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone,code,10, TimeUnit.MINUTES);

        try {
            thirdPartFeignService.sendCode(phone, code);
        } catch (Exception exception) {
            log.info("远程接口调用异常");
        }
        return ResultUtils.ok();
    }

    @PostMapping("/register")
    public String register(UserRegisterVo vo) {
        //TODO 手机短信验证

        try {
            ResultUtils r = memberFeignService.register(vo);
            log.info("注册远程调用：{}",r);
        } catch (Exception exception) {
            log.info("注册失败,错误原因：{}",exception);
            return "redirect:http://auth.lzyan.com/reg.html";
        }
        log.info("注册成功");
        return "redirect:http://auth.lzyan.com/login.html";
    }

    @GetMapping("/login.html")
    public String LoginPage(HttpSession session){
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if(attribute==null){
            //没登录
            return "login";
        }else {
            //已登录
            return "redirect:http://lzyan.com";
        }
    }

    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes redirectAttributes, HttpSession session){
        //远程登录
        ResultUtils r = memberFeignService.login(vo);
        if(r.getCode()==0){
            MemberRespVo data = (MemberRespVo) r.getBean("data", MemberRespVo.class);
            //成功 放入session中
            session.setAttribute(AuthServerConstant.LOGIN_USER,data);
            return "redirect:http://lzyan.com";
        }else {
            Map<String,String> errors = new HashMap<>();
            String msg = r.getJson("msg");
            errors.put("msg",msg);
            log.info("错误原因：{}",msg);
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.lzyan.com/login.html";
        }
    }
}
